package com.example.list.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.list.R;
import com.example.list.model.Pessoa;

public class PessoaAdapter extends BaseAdapter {

	LayoutInflater mInflater;
	ArrayList<Pessoa> itens;

	public PessoaAdapter(Context context, ArrayList<Pessoa> itens) {
		this.itens = itens;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return itens.size();
	}

	@Override
	public Object getItem(int position) {
		return itens.get(position);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ItemHolder itemHolder;

		if (view == null) {

			view = mInflater.inflate(R.layout.item_adapter, null);

			itemHolder = new ItemHolder();
			itemHolder.nome = (TextView) view.findViewById(R.id.nome);
			itemHolder.sobrenome = (TextView) view.findViewById(R.id.sobrenome);

			view.setTag(itemHolder);
		} else {
			itemHolder = (ItemHolder) view.getTag();
		}

		if (position % 2 == 0) {
			view.setBackgroundResource(R.drawable.dark_gray);
		} else {
			view.setBackgroundResource(R.drawable.gray);
		}

		Pessoa pessoa = itens.get(position);
		itemHolder.nome.setText(pessoa.getNome());
		itemHolder.sobrenome.setText(pessoa.getSobrenome());

		return view;
	}

	private class ItemHolder {
		TextView nome;
		TextView sobrenome;
	}
}
